//
//  ZMAlertView.m
//  MapTest
//
//  Created by apple on 16/4/29.
//  Copyright © 2016年 apple-mushao. All rights reserved.
//
#define Title_Font [UIFont systemFontOfSize:17]
#define Message_Font [UIFont systemFontOfSize:14]
#import "ZMAlertView.h"

@implementation ZMAlertView

+(void)sharedAlertViewWithTitle:(NSString *)title andMessage:(NSString *)message andSureBtnTitle:(NSString *)sureTitle sureBtnIsDestructive:(BOOL)sureDestructive andCancleTitle:(NSString *)cancleTitle cancleBtnIsDestructive:(BOOL)cancleDestrcutive SureBlock:(void(^)())sureblock CancleBlock:(void(^)())cancleBlock
{
    CGFloat alertWidth = ([UIScreen mainScreen].bounds.size.width*3/4>290)?290:[UIScreen mainScreen].bounds.size.width*3/4;
    CGFloat titleHeight = [self getLabelHeight:title font:Title_Font width:alertWidth]>20?[self getLabelHeight:title font:Title_Font width:alertWidth]:20;
    CGFloat messageHeight = [self getLabelHeight:message font:Message_Font width:alertWidth]>20?[self getLabelHeight:message font:Message_Font width:alertWidth]:20;
    CGFloat height = 0.0;
    
    if(!sureTitle&&!cancleTitle)
    {
        height = titleHeight+messageHeight + 16+15;
    }else
    {
        height = titleHeight+messageHeight + 16+15+44;
    }
    UIWindow * mainWindow = [[UIApplication sharedApplication]keyWindow];
    UIView * backView = [[UIView alloc]initWithFrame:mainWindow.bounds];
    backView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    backView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    [mainWindow addSubview:backView];
    
    ZMAlertView * alertView = [[[self class] alloc]initWithFrame:CGRectMake(0, 0,alertWidth, height) WithTitle:title andMessage:message andSureTitle:sureTitle sureBtnIsDestructive:sureDestructive andCancleTitle:cancleTitle cancleBtnIsDestructive:cancleDestrcutive];
    alertView.backgroundColor = [UIColor whiteColor];
    alertView.center = CGPointMake(backView.frame.size.width/2.0, backView.frame.size.height/2.0);
    alertView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin;
    alertView.sureBlock = sureblock;
    alertView.cancleBlock = cancleBlock;
    [backView addSubview:alertView];
    
    
}
-(id)initWithFrame:(CGRect)frame WithTitle:(NSString *)title andMessage:(NSString *)message andSureTitle:(NSString *)sureTitle sureBtnIsDestructive:(BOOL)sureDestructive andCancleTitle:(NSString *)cancleTitle cancleBtnIsDestructive:(BOOL)cancleDestrcutive
{
    if (self = [super initWithFrame:frame])
    {
        self.layer.cornerRadius=5;
        self.clipsToBounds=YES;
        _titleLabl = [[UILabel alloc]initWithFrame:CGRectZero];
        _titleLabl.textAlignment = NSTextAlignmentCenter;
        _titleLabl.textColor=[UIColor blackColor];
        _titleLabl.font= Title_Font;
        _titleLabl.text = title;
        _titleLabl.numberOfLines = 0;
        
        _messageLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        _messageLabel.textAlignment = NSTextAlignmentCenter;
        _messageLabel.numberOfLines = 0;
        _messageLabel.text = message;
        _messageLabel.textColor=[UIColor blackColor];
        _messageLabel.font = Message_Font;
        
        
        _lineView = [[UIView alloc]initWithFrame:CGRectZero];
        _lineView.backgroundColor = LIGHTGRAY;
        
        
        _leftBtn = [[UIButton alloc]initWithFrame:CGRectZero];
        if (!sureTitle)
        {
            _leftBtn.hidden = YES;
        }
        [_leftBtn setTitle:sureTitle forState:UIControlStateNormal];
        _leftBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_leftBtn addTarget:self action:@selector(sureButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
        if (sureDestructive)
        {
            [_leftBtn setTitleColor:BLUECOLOR forState:UIControlStateNormal];
        }
        else
        {
             [_leftBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
        
        _rightBtn = [[UIButton alloc]initWithFrame:CGRectZero];
        if (!cancleTitle)
        {
            _rightBtn.hidden = YES;
        }
        [_rightBtn setTitle:cancleTitle forState:UIControlStateNormal];
        _rightBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_rightBtn addTarget:self action:@selector(cancleButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
        if (cancleDestrcutive)
        {
            [_rightBtn setTitleColor:BLUECOLOR forState:UIControlStateNormal];

        }else
        {
            [_rightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
        
        _lineViewTwo = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 1.0, 0)];
        _lineViewTwo.backgroundColor = LIGHTGRAY;
        if(!cancleTitle&&!sureTitle)
        {
            _lineView.hidden = YES;
        }
        
        [self addSubview:_titleLabl];
        [self addSubview:_messageLabel];
        [self addSubview:_lineView];
        [self addSubview:_leftBtn];
        [self addSubview:_rightBtn];
        [self addSubview:_lineViewTwo];
        
        [self setUpConstraints];
        
    }
    return self;
}
#pragma mark-----------private
-(void)setUpConstraints
{
    _titleLabl.translatesAutoresizingMaskIntoConstraints = NO;
    _messageLabel.translatesAutoresizingMaskIntoConstraints = NO;
    _lineView.translatesAutoresizingMaskIntoConstraints = NO;
    _leftBtn.translatesAutoresizingMaskIntoConstraints = NO;
    _rightBtn.translatesAutoresizingMaskIntoConstraints = NO;
    _lineViewTwo.translatesAutoresizingMaskIntoConstraints = NO;
    
    NSArray * vertalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-15-[_titleLabl]-[_messageLabel]-10-[_lineView(0.5)]-[_leftBtn]-5-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_titleLabl,_messageLabel,_lineView,_leftBtn)];
    NSArray * titleConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[_titleLabl]-5-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_titleLabl)];
    NSArray * messageConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[_messageLabel]-5-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_messageLabel)];
    NSArray * lineViewConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_lineView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_lineView)];

    if (_leftBtn.hidden==YES||_rightBtn.hidden==YES)
    {
        _lineViewTwo.hidden = YES;
    }
    
    NSLayoutConstraint * leftBtnConstraintX = [NSLayoutConstraint constraintWithItem:_leftBtn attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:_rightBtn.hidden?1.0:0.5 constant:0.0];
   
    NSLayoutConstraint * leftBtnWidth = [NSLayoutConstraint constraintWithItem:_leftBtn attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeWidth multiplier:_rightBtn.hidden?1.0:0.5 constant:0.0];
    
    NSLayoutConstraint * rightConstraintX = [NSLayoutConstraint constraintWithItem:_rightBtn attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:_leftBtn.hidden?1.0:1.5 constant:0.0];
    NSLayoutConstraint * rightConstraintY = [NSLayoutConstraint constraintWithItem:_leftBtn attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:_rightBtn attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0];
    NSLayoutConstraint * rightBtnWidth = [NSLayoutConstraint constraintWithItem:_rightBtn attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeWidth multiplier:_leftBtn.hidden?1.0:0.5 constant:0.0];
    
    NSLayoutConstraint * lineTwoConstraintX = [NSLayoutConstraint constraintWithItem:_lineViewTwo attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0];
    NSLayoutConstraint * lineTwoConstraintY = [NSLayoutConstraint constraintWithItem:_lineViewTwo attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:_leftBtn attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0];
    NSLayoutConstraint * lineTwoConstraintHeight = [NSLayoutConstraint constraintWithItem:_lineViewTwo attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:_leftBtn attribute:NSLayoutAttributeHeight multiplier:0.0 constant:20.0];
    
    NSLayoutConstraint * lineTwoConstraintwidth = [NSLayoutConstraint constraintWithItem:_lineViewTwo attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:_leftBtn attribute:NSLayoutAttributeWidth multiplier:0.0 constant:0.5];
    
    NSArray * btnConstraints = @[leftBtnConstraintX,rightConstraintX,rightConstraintY,leftBtnWidth,rightBtnWidth,lineTwoConstraintX,lineTwoConstraintY,lineTwoConstraintHeight,lineTwoConstraintwidth];
    NSArray * constraintsAll = @[vertalConstraints,titleConstraints,messageConstraints,lineViewConstraints,btnConstraints];
   
    for (int i = 0 ; i < constraintsAll.count ; i++)
    {
        [self addConstraints:constraintsAll[i]];
    }

}
+ (CGFloat)getLabelHeight:(NSString *)string font:(UIFont *)font width:(CGFloat)width
{
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName,nil];
    CGSize textSize = CGSizeMake(width, 0.0f);
    textSize = [string boundingRectWithSize:textSize options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading  attributes:dict context:nil].size;
    return textSize.height;
}
-(void)cancleButtonTapped:(id)sender
{
    if (_cancleBlock)
    {
        _cancleBlock();
    }
    [self.superview removeFromSuperview];
}
-(void)sureButtonTapped:(id)sender
{
    if (_sureBlock)
    {
        _sureBlock();
    }
    [self.superview removeFromSuperview];
}

@end
